home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / amusemen / vgatarot.3 / vgatarot / VGATarot-0.3.fix / vgaaces.c < prev    next >
C/C++ Source or Header  |  1994-05-25  |  7KB  |  244 lines

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<strings.h>
  4. #include<unistd.h>
  5. #include<signal.h>
  6. #include<vga.h>
  7. #include<vgagl.h>
  8. #include<sys/time.h>
  9. #include<sys/types.h>
  10. #include<fcntl.h>
  11. #include"zsoftpcx.h"
  12.  
  13. typedef struct timeval t_tv;
  14. typedef struct timezone t_tz;
  15.     
  16. unsigned long giverand(unsigned long range,int seedadd);
  17.  
  18. void viewpcx(int XBeg,int YBeg,char *libname,char *indexname,int cnum,int orientation);
  19.  
  20. void north_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel);
  21.  
  22. void east_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel);
  23.  
  24. void south_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel);
  25.  
  26. void west_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel);        
  27.  
  28.  
  29.  
  30. void main(int argc,char *argv[]) {
  31.     char question[5000];
  32.     char name[200];
  33.     int hand[4][2] = {{0,0},{0,0},{0,0},{0,0}};
  34.     int done=0;
  35.     int deckx[78] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  36.              0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  37.              0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };    
  38.     int vgamode=G640x480x256;
  39.     char posit[4][80] = {"First","Second","Third","Fourth"};
  40.  
  41.     int pos[4][2]={{48,160},{196,160},{344,160},{492,160}};
  42.     int seed,x,y,z;
  43.  
  44.     printf("\nVGATarot - Aces Spread V0.3\n");
  45.     printf("\nPlease enter your name (first and last) > ");
  46.     gets(name);
  47.     printf("Please enter your question\n> ");
  48.     gets(question);
  49.     
  50.     for(x=0,seed=0;x!=strlen(name);x++,seed+=name[x]);
  51.     for(x=0;x!=strlen(question);x++,seed+=question[x]);
  52.     
  53.     for(z=0;z!=4;z++) {
  54.     done=0;
  55.     y=0;
  56.     while(!done) {
  57.         x=(int)giverand(78,seed);
  58.         if(deckx[x]==0) {
  59.             deckx[x]=1;
  60.             y++;
  61.             if((x==22)||(x==36)||(x==50)||(x==64)) done = 1;
  62.             if(y==13) done = 1; }
  63.         hand[z][0]=x;
  64.         if((int)giverand(100,seed)>50) hand[z][1]=1; else hand[z][1]=0; } }
  65.  
  66.     if(!vga_hasmode(vgamode)) {
  67.         printf("mode is unsupported\n"); 
  68.         exit(-1); }
  69.         
  70.     vga_setmode(vgamode);
  71.     gl_setcontextvga(vgamode);
  72.     gl_enableclipping();
  73.     gl_setwritemode(WRITEMODE_OVERWRITE);
  74.     
  75.     printf("\n");
  76.  
  77.     for(x=0;x!=4;x++) {
  78.         if(hand[x][1]==0) seed=1; else seed=3;
  79.         printf("%-20s : %s %s\n",posit[x],deck[hand[x][0]],hand[x][1]==0?"":"Reversed");
  80.         viewpcx(pos[x][0],pos[x][1],"tarotlib.lbr","tarotlib.index",hand[x][0],seed); }
  81.         
  82.     printf("\n");
  83.  
  84.     getchar();
  85.     vga_setmode(TEXT);
  86.     
  87.     exit(1); }
  88.     
  89. unsigned long giverand(unsigned long range,int seedadd)
  90.     { 
  91.     t_tv tv;
  92.     t_tz tz;
  93.         
  94.     gettimeofday(&tv,&tz);
  95.     srandom((unsigned int)tv.tv_usec+(unsigned int)seedadd);
  96.     return (unsigned long)((random()%range)+1); }
  97.  
  98. void viewpcx(int XBeg,int YBeg,char *libname,char *indexname,int cnum,int orientation) { 
  99.     int fi; /* index file */
  100.     int fp; /* data file */
  101.     unsigned char *data_pix;
  102.     unsigned long foffset;
  103.     unsigned long flength;
  104.     int XPos,YPos,XMax,YMax;
  105.     zhead pcx;
  106.     Palette pal;
  107.     unsigned long x;
  108.     unsigned int y,z,inbyte;
  109.     unsigned char inc;
  110.  
  111.     if((fi=open(indexname,O_RDONLY))==-1) {
  112.         vga_setmode(TEXT);
  113.         printf("cannot open index file\n");
  114.         exit(-1); }
  115.         
  116.     lseek(fi,cnum*8,SEEK_SET); /* get the correct index */
  117.     
  118.     read(fi,&foffset,sizeof(foffset)); /* get the offset */
  119.     read(fi,&flength,sizeof(flength)); /* get the length */
  120.     
  121.     close(fi); /* close index file */
  122.     
  123.     if((fp=open(libname,O_RDONLY))==-1) { /* open library file */
  124.         vga_setmode(TEXT);
  125.         printf("cannot open library file\n");
  126.         exit(-1); }
  127.     
  128.     lseek(fp,foffset,SEEK_SET); /* go to correct offset in library */
  129.             
  130.     if(read(fp,&pcx,sizeof(pcx))!=sizeof(pcx)) { /* read PCX header information */
  131.         vga_setmode(TEXT);
  132.         printf("cannot read header information of picture %d\n",cnum);
  133.         exit(-1); }
  134.         
  135.     XMax=(int)pcx.x2; YMax=(int)pcx.y2; /* set the x and y dimensions of the picture */
  136.     
  137.     lseek(fp,128-sizeof(pcx),SEEK_CUR);
  138.     
  139.     if((data_pix=(char *)malloc(flength-128))==NULL) { /* get picture memory */
  140.         vga_setmode(TEXT);
  141.         printf("couldn't allocate picture data memory\n");
  142.         exit(-1); }    
  143.         
  144.     if(((unsigned long)read(fp,data_pix,(flength-128)))!=(flength-128)) { /* read picture data */
  145.         vga_setmode(TEXT);
  146.         printf("couldn't read in picture data\n");
  147.         exit(-1); }
  148.         
  149.     close(fp); /* close library file */
  150.     
  151.     data_pix+=(flength-128-768); /* get to the palette data */
  152.                 
  153.     for(x=0;x<255;x++) { /* set the palette data */
  154.         inc=*data_pix++;
  155.         pal.color[x].red=(inc/4);
  156.         inc=*data_pix++;
  157.         pal.color[x].green=(inc/4);
  158.         inc=*data_pix++;
  159.         pal.color[x].blue=(inc/4); }
  160.         
  161.     data_pix-=(flength-131); /* why 131?  I've no damn clue, but it works better than 128 */
  162.         
  163.     gl_setpalette(&pal); /* set the palette */
  164.     
  165.     switch(orientation) { /* set up correct orientation parameters */
  166.         case 1 : /* north */ 
  167.                     XMax=(int)pcx.x2+1;
  168.                     YMax=(int)pcx.y2;
  169.                     XPos=XBeg; 
  170.                     YPos=YBeg; 
  171.                     break;
  172.         case 2 : /* east */ 
  173.                     XMax=(int)pcx.x2+1;
  174.                     YMax=(int)pcx.y2;
  175.                     XPos=YBeg+YMax;
  176.                     YPos=XBeg;
  177.                     break;
  178.         case 3 : /* south */ 
  179.                     XMax=(int)pcx.x2+1;
  180.                     YMax=(int)pcx.y2;
  181.                     XPos=XBeg+XMax; 
  182.                     YPos=YBeg+YMax; 
  183.                     break;
  184.         case 4 : /* west */ 
  185.                     XMax=(int)pcx.x2+1;
  186.                     YMax=(int)pcx.y2;
  187.                     XPos=YBeg;
  188.                     YPos=XBeg+XMax;
  189.                     break; }
  190.     
  191.     for(x=0;x<(XMax*YMax);) { /* decode the picture */
  192.         inbyte=(unsigned int)*data_pix++;
  193.         if(inbyte>0xc0) { /* RLE compression */
  194.             inbyte-=0xc0;
  195.             y=(unsigned int)*data_pix++;
  196.             for(z=0;z<inbyte;z++) {
  197.                 x++;
  198.                 switch(orientation) {
  199.                     case 1 : north_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,y); break;
  200.                     case 2 : east_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,y); break;                    
  201.                     case 3 : south_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,y); break;
  202.                     case 4 : west_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,y); break; } } }
  203.         else {
  204.             x++;
  205.             switch(orientation) {
  206.                 case 1 : north_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,inbyte); break;
  207.                 case 2 : east_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,inbyte); break;                                    
  208.                 case 3 : south_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,inbyte); break;
  209.                 case 4 : west_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,inbyte); break; } } }
  210.                 
  211.         free(data_pix); } /* free the data, we are done */
  212.  
  213. void north_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel) {
  214.     if(*y>(ym+yb)) return;
  215.     gl_setpixel(*x,*y,pixel);
  216.     *x=*x+1;
  217.     if(*x==(xm+xb)) {
  218.         *x=xb;
  219.         *y=*y+1; } }
  220.         
  221. void south_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel) { 
  222.     if(*y==yb) return;
  223.     gl_setpixel(*x,*y,pixel);
  224.     *x=*x-1;
  225.     if(*x==xb) {
  226.         *x=xm+xb;
  227.         *y=*y-1; } }        
  228.         
  229. void west_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel) {
  230.     if(*x>(ym+yb)) return;
  231.     gl_setpixel(*x,*y,pixel);
  232.     *y=*y-1;
  233.     if(*y==xb) {
  234.         *y=xb+xm;
  235.         *x=*x+1; } }
  236.         
  237. void east_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel) {
  238.     if((*x)<yb) return;
  239.     gl_setpixel(*x,*y,pixel);
  240.     *y=*y+1;
  241.     if(*y==(xm+xb)) {
  242.         *y=xb;
  243.         *x=*x-1; } }
  244.